package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

import Models.POSProductServingType;
import Models.POSProducts;

public class POSProductsPresenter {
	public ArrayList<POSProducts> tryJSONParseArray(String input){
		ArrayList<POSProducts> products = new ArrayList<POSProducts>();
		try{
			JSONObject jsonObj = null;
			jsonObj = XML.toJSONObject(input);
			JSONObject jsonAllObjects = jsonObj.getJSONObject("pOSProductss");
				if(!jsonAllObjects.isNull("posProducts")){
					Object item = jsonAllObjects.get("posProducts");
					if(item instanceof JSONArray){
						JSONArray jArray = (JSONArray)item;
						for(int i=0; i < jArray.length(); i++){
							JSONObject jObject = jArray.getJSONObject(i);
							products.add(createProductFromJSONObject(jObject));
						}
					} else {
						JSONObject jObject = (JSONObject)item;
						products.add(createProductFromJSONObject(jObject));
					}
				}
			return products;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSProducts tryJSONParse(String input){
		try{
			JSONObject jsonObj = null;
			jsonObj = XML.toJSONObject(input);
			JSONObject jsonHop = jsonObj.getJSONObject("posProducts");
			POSProducts product = createProductFromJSONObject(jsonHop);
			return product;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public POSProducts createProductFromJSONObject(JSONObject jsonProduct){
		POSProducts product = new POSProducts();
		try{
			product.setId(jsonProduct.getInt("id"));
			product.setDescription(jsonProduct.getString("productDescription"));
			product.setOnTap(jsonProduct.getBoolean("productOnTap"));
			//Build Beer
			POSBeerViewPresenter beerPresenter = new POSBeerViewPresenter();
			JSONObject jsonBeer = jsonProduct.getJSONObject("productBeer");
			product.setBeer(beerPresenter.createBeerFromJSONObject(jsonBeer));
			//Set POSProductServingTypeCollection.
			ArrayList<POSProductServingType> psts = new ArrayList<POSProductServingType>();
			if(!jsonProduct.isNull("servingTypeCollection")){
				Object item = jsonProduct.get("servingTypeCollection");
				POSProductServingTypePresenter pstPresenter = new POSProductServingTypePresenter();
				if(item instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)item;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonPST = jPSTArray.getJSONObject(i);
						psts.add(pstPresenter.createPSTFromJSONObject(jsonPST));
					}
				} else {
					JSONObject jsonPST = (JSONObject)item;
					psts.add(pstPresenter.createPSTFromJSONObject(jsonPST));
				}
				product.setProductServingTypes(psts);
			}
			return product;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(POSProducts item){
		try{
			JSONObject json = new JSONObject();
			json.put("id", item.getId());
			json.put("productDescription", item.getDescription());
			json.put("productOnTap", item.isOnTap());
			POSBeerViewPresenter beerPresenter = new POSBeerViewPresenter();
			json.put("productBeer", beerPresenter.createJSON(item.getBeer()));
			if(item.getProductServingTypes()!=null){
				POSProductServingTypePresenter pstPresenter = new POSProductServingTypePresenter();
				if(item.getProductServingTypes().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getProductServingTypes().size(); i++){
						jsonArray.put(pstPresenter.createJSON(item.getProductServingTypes().get(i)));
					}
					json.put("servingTypeCollection", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
}
